package com.ruoyi.apis.controller;

import com.ruoyi.apis.entity.BannerEntity;
import com.ruoyi.apis.entity.OfflineEntity;
import com.ruoyi.apis.entity.OnlineEntity;
import com.ruoyi.apis.entity.VideoEntity;
import com.ruoyi.apis.service.BannerService;
import com.ruoyi.apis.service.OfflineService;
import com.ruoyi.apis.service.OnlineService;
import com.ruoyi.apis.service.VideoService;
import com.ruoyi.apis.vo.*;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.HashMap;
import java.util.List;

/**
 * @Author Choi
 * @Date 2021/7/19
 */
@Api(value = "API接口", tags = "案例项目接口")
@RestController
@RequestMapping("/api")
@Validated
public class ProjectController {

    @Autowired
    private BannerService bannerService;
    @Autowired
    private OnlineService onlineService;
    @Autowired
    private OfflineService offlineService;
    @Autowired
    private VideoService videoService;

    /**
     * 首页海报
     */
    @GetMapping("/banners")
    @ApiOperation(value = "首页海报接口")
    public AjaxResult banner() {
        List<BannerEntity> banners = bannerService.getBanners();
        List<BannerVO> list = BannerVO.getBannerList(banners);
        return AjaxResult.success(list);
    }

    /**
     * 线上项目
     */
    @GetMapping("/onlines")
    @ApiOperation(value = "线上项目接口")
    public AjaxResult online(@ApiParam(name = "page", value = "页码", required = true)
                             @RequestParam(defaultValue = "0") int page,
                             @ApiParam(name="count", value = "每页显示的数量", required = true)
                             @RequestParam(defaultValue = "15") int size) {
        Page<OnlineEntity> onlines = onlineService.getOnlines(page, size);
        List<OnlineVO> list = OnlineVO.getOnlineList(onlines.getContent());
        PageVO pageVO = new PageVO(list, onlines.getNumber(), onlines.getSize(), onlines.getTotalElements());
        return AjaxResult.success(pageVO);
    }

    /**
     * 线上项目详情
     */
    @ApiOperation(value = "线上项目详情")
    @GetMapping("/onlines/{onlineId}")
    public AjaxResult onlineDetail(@PathVariable @Min(value = 1) long onlineId) {
        OnlineVO onlineVO = onlineService.getById(onlineId);
        return AjaxResult.success(onlineVO);
    }

    /**
     * 线下项目
     */
    @GetMapping("/offlines")
    @ApiOperation(value = "线下项目接口")
    public AjaxResult offline(@ApiParam(name = "page", value = "页码", required = true)
                              @RequestParam(defaultValue = "0") int page,
                              @ApiParam(name="count", value = "每页显示的数量", required = true)
                              @RequestParam(defaultValue = "15") int size) {
        Page<OfflineEntity> offlines = offlineService.getOfflines(page, size);
        List<OfflineVO> list = OfflineVO.getOfflineList(offlines.getContent());
        PageVO pageVO = new PageVO(list, offlines.getNumber(), offlines.getSize(), offlines.getTotalElements());
        return AjaxResult.success(pageVO);
    }

    /**
     * 线下项目详情
     */
    @ApiOperation(value = "线下项目详情")
    @GetMapping("/offlines/{offlineId}")
    public AjaxResult offlineDetail(@PathVariable @Min(value = 1) long offlineId) {
        OfflineVO offlineVO = offlineService.getById(offlineId);
        return AjaxResult.success(offlineVO);
    }

    /**
     * 视频案例
     */
    @GetMapping("/videos")
    @ApiOperation(value = "视频案例接口")
    public AjaxResult video(@ApiParam(name = "page", value = "页码", required = true)
                            @RequestParam(defaultValue = "0") int page,
                            @ApiParam(name="count", value = "每页显示的数量", required = true)
                            @RequestParam(defaultValue = "15") int size) {
        Page<VideoEntity> videos = videoService.getVideos(page, size);
        List<VideoVO> list = VideoVO.getVideoList(videos.getContent());
        PageVO pageVO = new PageVO(list, videos.getNumber(), videos.getSize(), videos.getTotalElements());
        return AjaxResult.success(pageVO);
    }

    /**
     * 视频案例详情
     */
    @ApiOperation(value = "视频案例详情")
    @GetMapping("/videos/{videoId}")
    public AjaxResult videoDetail(@PathVariable @Min(value = 1) long videoId) {
        VideoVO videoVO = videoService.getById(videoId);
        return AjaxResult.success(videoVO);
    }

    /**
     * 搜索项目
     */
    @GetMapping("/search")
    @ApiOperation(value = "搜索项目接口")
    public AjaxResult search(@ApiParam(name = "title", value = "主题")
                             @RequestParam(name = "title", defaultValue = "") String title) {
        title = "%" + title + "%";
        HashMap<String, Object> map = new HashMap<>();
        List<OnlineEntity> onlines = onlineService.getByTitle(title);
        List<OfflineEntity> offlines = offlineService.getByTitle(title);
        List<VideoEntity> videos = videoService.getByTitle(title);
        map.put("online", OnlineVO.getOnlineList(onlines));
        map.put("offline", OfflineVO.getOfflineList(offlines));
        map.put("video", VideoVO.getVideoList(videos));
        return AjaxResult.success(map);
    }
}
